package com.cs.filter;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

@Configuration
public class RequestRateLimitConfig {

    /**
     * 根据ip进行限流
     * @return
     */
    @Bean
    @Primary
    public KeyResolver hostAddrKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getHeaders().getHost().getHostName());
    }

    /**
     * 根据接口的路径限流
     * @return
     */
    @Bean
    public KeyResolver apiResolver(){
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }
}
